package org.ayachinene.leetcode.utils;

import org.jetbrains.annotations.NotNull;
import org.springframework.util.CollectionUtils;

import java.util.List;

public class ArrayUtils {

    public static int @NotNull [] getIntArray(@NotNull String s) {
        List<String> elementStrings = CommonUtils.extract(s);
        // When s is "[]"
        if (CollectionUtils.isEmpty(elementStrings)) {
            return new int[0];
        }
        return elementStrings.stream()
                .mapToInt(Integer::parseInt)
                .toArray();
    }

    public static int @NotNull [][] getIntArray2D(@NotNull String s) {
        List<String> arrayStrings = CommonUtils.extract2D(s);
        if (CollectionUtils.isEmpty(arrayStrings)) {
            return new int[0][];
        }
        return arrayStrings.stream()
                .map(ArrayUtils::getIntArray)
                .toArray(int[][]::new);
    }

    public static String @NotNull [] getStringArray(@NotNull String s) {
        return CommonUtils.extract(s).stream()
                .map(str -> str.substring(1, str.length() - 1))
                .toArray(String[]::new);
    }
}
